<?php

namespace app\api\service;

use think\Controller;
use think\Db;
use think\Exception;

class Product extends Controller
{
    /**
     * 获取店铺补货产品列表
     * @param $param
     * @return array
     */
    public static function get_shop_replenishment_product_list($param)
    {
        try {
            $list = db('product')->alias('p')->field('p.thumb, p.name as p_name, p.price, p.id, b.name as b_name, sp.inventory, sraf.status')
                ->join('jx_shop_product sp', 'sp.product_id = p.id AND sp.shop_id = ' . $param['shop_id'], 'LEFT')
                ->join('jx_shop_replenishment_apply_for sraf', 'sraf.product_id = p.id AND sraf.shop_id = ' . $param['shop_id'] . ' AND sraf.status < 3', 'LEFT')
                ->join('jx_brand b', 'b.id = p.brand_id')
                ->where('p.category_id', $param['category_id'])
                ->where('p.status', 2)
                ->where('p.delete', 1)
                ->order('sp.inventory desc')
                ->paginate();

            $data = [];
            if ($list) {
                foreach ($list as $v) {
                    $data['list'][] = [
                        'thumb' => get_img_url($v['thumb']),
                        'product_name' => $v['p_name'],
                        'brand_name' => $v['b_name'],
                        'price' => $v['price'],
                        'id' => $v['id'],
                        'status' => $v['status'] > 0 ? '补货中' : ($v['inventory'] > 10 ? '库存充足' : '库存告急'),
                        'inventory' => $v['inventory'] | 0
                    ];
                }
                $list = $list->toArray();
                $is_more = $list['total'] - ($list['current_page'] * $list['per_page']);

                $data['is_more'] = $is_more;
            }
            return api_return(200, $data);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺需要补货的产品详情
     * @param $param
     * @return array
     */
    public static function get_replenishment_product_info($param) {
        try {
            $product = model('product')->get(['id' => $param['id'], 'status' => 2, 'delete' => 1]);
            $data = [
                'name' => $product->name,
                'thumb' => get_img_url($product->thumb),
                'brand_name' => $product->brand->name,
            ];
            return api_return(200, $data);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 店铺提交商品补货申请
     * @param $param
     * @return array
     */
    public static function shop_product_replenishment_apply_for($param) {
        try {
            $shop = model('shop')->get(['member_id' => $param['member_id'], 'id' => $param['shop_id']]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }
            // 获取最后一条补货申请，如果状态是补货中，则提醒商家
            $replenishment_apply_for = model('shop_replenishment_apply_for')->get(['shop_id' => $param['shop_id'], 'product_id' => $param['product_id']]);
            if ($replenishment_apply_for && $replenishment_apply_for->status != 3 && $replenishment_apply_for->status != 4) {
                throw new \Exception('该商品正在补货中，请勿重复申请');
            }
            if ($param['num'] > 50) {
                throw new \Exception('补货数量不能大于50');
            }
            $data = [
                'product_id' => $param['product_id'],
                'shop_id' => $param['shop_id'],
                'num' => $param['num'],
                'msg' => $param['msg']
            ];
            model('shop_replenishment_apply_for')->save($data);
            return api_return(200, []);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺产品列表
     * @param $param
     * @return array
     */
    public static function get_shop_product_list($param) {
        try {
            $shop = model('shop')->get(['id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            if ($param['page_type'] == 1) {  // 1表示从个人中心商家入口进来， 拿的是分类下面的所有产品
                $list = db('product')->alias('p')->field('p.thumb, p.name, p.price, p.id, b.name as brand_name, sp.inventory')
                    ->join('jx_shop_product sp', 'sp.product_id = p.id AND sp.shop_id = ' . $param['shop_id'], 'LEFT')
                    ->join('jx_brand b', 'b.id = p.brand_id', 'LEFT')
                    ->where('p.category_id', $param['category_id'])
//                ->where('sp.inventory', 'gt', 0)
                    ->where('p.status', 2)
                    ->where('p.delete', 1)
                    ->paginate();
            } else {  // 否则表示从酒柜入口进来， 只拿店铺在该分类下面的有库存的产品
                $list = db('product')->alias('p')->field('p.thumb, p.name, p.price, p.id, b.name as brand_name, sp.inventory')
                    ->join('jx_shop_product sp', 'sp.product_id = p.id AND sp.shop_id = ' . $param['shop_id'], 'LEFT')
                    ->join('jx_brand b', 'b.id = p.brand_id', 'LEFT')
                    ->where('p.category_id', $param['category_id'])
                    ->where('sp.inventory', 'gt', 0)
                    ->where('p.status', 2)
                    ->where('p.delete', 1)
                    ->paginate();
            }


            $data = [];
            if ($list) {
                foreach ($list as $v) {
                    $data['list'][] = [
                        'thumb' => get_img_url($v['thumb']),
                        'product_name' => $v['name'],
                        'price' => $v['price'],
                        'id' => $v['id'],
                        'brand_name' => $v['brand_name'],
                        'inventory' => $v['inventory'],
                    ];
                }
                $list = $list->toArray();
                $is_more = $list['total'] - ($list['current_page'] * $list['per_page']);

                $data['is_more'] = $is_more;
            }
            return api_return(200, $data);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }

    /**
     * 获取店铺产品详情
     * @param $param
     * @return array
     */
    public static function get_shop_product_info($param) {
        try {
            $shop = model('shop')->get(['id' => $param['shop_id'], 'status' => 2]);
            if (!$shop) {
                throw new \Exception('店铺不存在！');
            }

            $shop_product = model('shop_product')->get(['product_id' => $param['id'], 'shop_id' => $param['shop_id']]);
            if (!$shop_product) {
                throw new \Exception('产品不存在');
            }

            $data = [
                'id' => $shop_product->product_id,
                'name' => $shop_product->product->name,
                'price' => $shop_product->product->price,
                'counter_price' => $shop_product->product->counter_price,
                'date_in_produced' => date('Y年m月d日', $shop_product->product->date_in_produced),
                'place_of_origin' => $shop_product->product->place_of_origin,
                'degree' => $shop_product->product->degree,
                'ml' => $shop_product->product->ml,
                'scent' => $shop_product->product->scent ?: '无' ,
                'brand_name' => $shop_product->product->brand->name,
                'content' => $shop_product->product->details->content,
                'content_url' => 'http://jx.mgtvshop.com/index/base/product_content/id/' . $param['id'],
                'shop_name' => $shop->name,
                'member_id' => $shop->member_id,
                'shop_logo' => get_img_url($shop->logo),
            ];

            $images = $shop_product->product->images()->order('is_main desc')->select();
            $img_data = [];
            foreach ($images as $k => $v) {
                $img_data[] = [
                    'thumb' => get_img_url($v->thumb),
                    'image' => get_img_url($v->image)
                ];
            }
            $data['images'] = $img_data;
            return api_return(200, $data);
        } catch (\Exception $e) {
            Db::rollback();
            return api_return(0, '', $e->getMessage());
        }
    }
}
